package com.kgcx.dbutils.domain;

import com.kgcx.common.bean.RequestData;
import com.kgcx.dbutils.annotation.SelectBetweenAnd;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.util.ReflectUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OrderBy;
import java.sql.Timestamp;

/**
 * @author 吴刚林
 * @date 2017/11/23
 */
@Data
public class BaseDomain {
    @Id
    @GeneratedValue(generator = "JDBC")
    @ApiModelProperty(value = "主键，新增不填", example = "1", readOnly = true)
    private Long kid;
    /**
     * 逻辑状态：101=启用，102=禁用，103=删除
     */
    @ApiModelProperty(value = "逻辑状态：101=启用，102=禁用，103=删除", required = true, example = "101", readOnly = true)
    private Integer status;
    /**
     * 记录创建人id
     */
    @ApiModelProperty(value = "记录创建人id", example = "1", readOnly = true)
    private Integer createId;
    /**
     * 记录创建时间
     */
    @ApiModelProperty(value = "记录创建时间", example = "1511248354000", readOnly = true)
    @SelectBetweenAnd
    @OrderBy("DESC")
    private Timestamp createTime;
    /**
     * 记录修改人id
     */
    @ApiModelProperty(value = "记录修改人id", example = "1", readOnly = true)
    private Integer updateId;
    /**
     * 记录修改时间
     */
    @ApiModelProperty(value = "记录修改时间", example = "1511248354000", readOnly = true)
    private Timestamp updateTime;


    public static <T> T getInstance(Class<? extends BaseDomain> c) {
        BaseDomain domain = ReflectUtil.newInstance(c);
        domain.setStatus(101);
        Timestamp now = new Timestamp(System.currentTimeMillis());
        domain.setCreateTime(now);
        domain.setUpdateTime(now);
        return (T) domain;
    }


//    @Transient
//    private List<Long> cityIds;

    //    public List<Long> getCityIds() {
//        return cityIds;
//    }
//
//    public void setCityIds(List<Long> cityIds) {
//        this.cityIds = cityIds;
//    }


    public static <T> T buildSelectDomain(RequestData rd, T domain) {
        if (MapUtils.isNotEmpty(rd) && null != domain) {
            domain = BeanUtil.fillBeanWithMap(rd, domain, true);
            /*List<Field> fieldList = new ArrayList<>(Arrays.asList(domain.getClass().getDeclaredFields()));
            Class<?> superclass = domain.getClass().getSuperclass();

            if (null != superclass) {
                fieldList.addAll(Arrays.asList(superclass.getDeclaredFields()));
            }
            if (CollectionUtils.isNotEmpty(fieldList)) {
                rd.forEach((k, v) -> {
                    fieldList.stream()
                            .filter(f -> k.equals(f.getName()))
                            .forEach(
                                    f -> {
                                        Object value = v;
                                        if (List.class.isAssignableFrom(f.getType())) {
                                            value = Arrays.asList(StringUtils.split(value.toString(), ","));
                                        }
                                        ReflectHelper.setFieldValue(domain, f.getName(), TypeUtils.cast(value, f.getType(), null));
                                    }
                            );
                });
            }*/
        }
        return domain;
    }

    public static <T> T buildSelectDomain(RequestData rd, Class<T> domainClass) {
        return buildSelectDomain(rd, ReflectUtil.newInstance(domainClass));
    }
}
